package com.springboot.love.shiro.realm;

import com.springboot.love.entity.AdminUser;
import com.springboot.love.service.AdminUserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;


import javax.annotation.Resource;

public class DbRealm extends AuthorizingRealm {
    @Resource
    private AdminUserService adminUserService;

    @Override
    public String getName() {
        return "DbRealm";
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username= (String) token.getPrincipal();
        AdminUser adminUser=this.adminUserService.queryByUsername(username);
        if (adminUser == null) {//需要改，存在很大问题
//            throw new UnknownAccountException("账号不存在！");
            return null;
        }
        return new SimpleAuthenticationInfo(
                adminUser,
                adminUser.getPassword(),
                ByteSource.Util.bytes(adminUser.getSalt()),
                getName()
        );
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
}
